package com.robot.member.controller;

import com.alibaba.fastjson.JSON;
import com.robot.member.pojo.AgentApply;
import com.robot.member.service.AgentApplyService;
import entity.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 代理商入驻申请agent_apply控制器层
 *
 * @author lin
 */
@RestController
@CrossOrigin
@RequestMapping("/agent_apply")
public class AgentApplyJsonController {
    @Autowired
    private AgentApplyService applyService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 条件查询AgentApply列表并分页
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/third/search/{page}/{size}", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String thirdFindAgentApplyBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<AgentApply> pageList = applyService.findSearch(searchMap, page, size);
        logger.info("agentApplyPage=" + JSON.toJSONString(pageList));
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据id查询AgentApply
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/third/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String thirdFindAgentApplyById(@PathVariable String id) {
        AgentApply agentApplyInDb = applyService.findById(id);
        logger.info("agentApplyInDb=" + agentApplyInDb);
        return JSON.toJSONString(agentApplyInDb);
    }

    /**
     * 根据id和AgentApply修改AgentApply
     *
     * @param agentApply
     * @param id
     */
    @RequestMapping(value = "/third/{id}", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public void thirdUpdateAgentApplyByAgentApply(@RequestBody String agentApply, @PathVariable String id) {
        AgentApply agentApply1 = JSON.parseObject(agentApply, AgentApply.class);
        agentApply1.setId(id);
        applyService.update(agentApply1);
    }
}
